package com.foreknow.service.impl;

import com.foreknow.bean.Admin;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.AdminService;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by foreknow on 2022/6/27.
 */
public class AdminServiceImpl implements AdminService {
    @Override
    public Admin isCheckLogin(String username, String password) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        adminDao.setConnection(conn);
        return adminDao.login(username,password);
    }

    @Override
    public boolean save(Admin admin) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        adminDao.setConnection(conn);
        boolean isRight = false;
        //调用adminDao中的方法并处理事务
        try {
            //调用adminDao中的方法
            isRight = adminDao.register(admin);
            //提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                //回滚
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
            return isRight;
    }

    public static void main(String[] args) {
        AdminService service = new AdminServiceImpl();
        Admin admin = service.isCheckLogin("suhr","suhr");
        System.out.println(admin.getUsername()+"-----"+admin.getPassword());
    }
}
